package com.controller;

import com.bean.Arg.GetArticles;
import com.bean.ArticleArea;
import com.bean.ArticleContent;
import com.bean.ArticleInfo;
import com.dto.Execution;
import com.dto.WebResult;
import com.enums.ArticleEnums;
import com.github.pagehelper.PageHelper;
import com.service.ArticleAreaService;
import com.service.ArticleContentService;
import com.service.ArticleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * Created by Yuicon on 2016/11/3.
 */
@RestController
@CrossOrigin(origins = {"*"}, methods = {RequestMethod.OPTIONS,RequestMethod.POST,RequestMethod.GET})
public class ArticleController {

    @Autowired
    private ArticleInfoService articleInfoService;

    @Autowired
    private ArticleAreaService articleAreaService;

    @Autowired
    private ArticleContentService articleContentService;

    @RequestMapping("/getArticles")
    public WebResult getArticles(@RequestBody ArticleInfo articleInfo){
        PageHelper.startPage(articleInfo.getPage(),3);//第一个参数是第几页；第二个参数是每页显示条数。
        return new WebResult<>(true,new Execution<>(ArticleEnums.GET_ARTICLEINFO_SUCCESS,articleInfoService.findAll(articleInfo.getAaid())));
    }

    @RequestMapping("/getArticle")
    public WebResult getArticle(@RequestBody ArticleInfo articleInfo){
        articleInfo = articleInfoService.findById(articleInfo.getId());
        ArticleContent articleContent = articleContentService.findByAid(articleInfo.getId());
        articleInfo.setContent(articleContent.getContent());
        return new WebResult<>(true,new Execution<>(ArticleEnums.GET_ARTICLE_SUCCESS,articleInfo));
    }

    @RequestMapping("/getArticleAreas")
    public WebResult getArticleAreas(){
        return new WebResult<>(true,new Execution<>(ArticleEnums.GET_ARTICLEAREA_SUCCESS,articleAreaService.findAll()));
    }

    @RequestMapping("/insertArticle")
    public void insertArticle(@RequestBody ArticleInfo articleInfo){
        String aid = UUID.randomUUID().toString();
        articleInfoService.insert(aid,articleInfo.getAaid(), articleInfo.getTitle(), articleInfo.getUploader(), articleInfo.getSynopsis());
        articleContentService.insert(aid,articleInfo.getContent());
    }
}
